package cm.mefobe.android.telecomcost.view;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableRow;
import android.widget.TextView;

public class PickerView extends TableRow {

	private Context _context;
	private TextView _label;
	private EditText _txt;
	private Button _btn;
	
	public PickerView(Context context) {
		super(context);
		Log.i("PickerView", "creating the picker view");
		_context = context;
	}

	public Button getButton() {
		if (_btn == null) {
			throw new UnsupportedOperationException(
					"PickerView button is not set");
		}

		return _btn;
	}

	public EditText getEditText() {
		if (_txt == null) {
			throw new UnsupportedOperationException(
					"PickerView edit text is not set");
		}

		return _txt;
	}

	public void setLabel(String label) {
		if (_label == null) {
			setPickerView();
		}

		_label.setText(label);
	}

	private void setPickerView() {
		if (isInEditMode()) {
			Log.i("PickerView", "isInEditMode");
		} else {
			if (_label == null) {
				Log.i("PickerView", "adding views...");
				//row = new TableRow(_context);

				_label = new TextView(_context);
				_txt = new EditText(_context);
				_btn = new Button(_context);
				_btn.setText("OK");
				_label.setText("Number");

				addView(_label);
				addView(_txt);
				addView(_btn);
				
				//addView(row);				
			} else {
				Log.i("PickerView", "PickerView already set...");
			}
		}
	}

	public PickerView(Context context, AttributeSet attrs) {
		super(context, attrs);
		Log.i("PickerView", "creating the picker view with AttributeSet");

		_context = context;
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		Log.i("PickerView", "onLayout");
		super.onLayout(changed, l, t, r, b);
		setPickerView();
	}

	@Override
	protected void onAttachedToWindow() {
		Log.i("PickerView", "onAttachedToWindow");
		super.onAttachedToWindow();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Log.i("PickerView", "onDraw");
		super.onDraw(canvas);
	}

	@Override
	protected void onFinishInflate() {
		Log.i("PickerView", "onFinishInflate");
		super.onFinishInflate();
		setPickerView();
	}

	@Override
	public void updateViewLayout(View view,
			android.view.ViewGroup.LayoutParams params) {
		Log.i("PickerView", "updateViewLayout");
		super.updateViewLayout(view, params);
	}

	public String getText() {
		return _txt.getText().toString();
	}

	public void setText(String text) {
		_txt.setText(text);
	}
}